<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\CurrencyLogic;
use Illuminate\Http\Request;

/**
 * Class CurrencyController
 * @package App\Http\Controllers\OA
 * @desc 币种控制器
 */
class CurrencyController extends Controller
{
    //币种列表
    public function currencyList(Request $request)
    {
        //获取线上的汇率转化
        $rules = [
            'currency'=>'sometimes|string|nullable',
            'page'=>'required|integer|min:1',
            'limit'=>'required|integer|min:10',
        ];

        $this->verify($request->all(),$rules);

        try{
            $logic = new CurrencyLogic();
            $list = $logic->getList($request);
            ret_json('ok','ok',$list);
        }catch (\Exception $e){
            //dd(11);
            ret_json('err',$e->getMessage());
        }

    }

    //修改汇率
    public function editRate(Request $request)
    {
        $rules = [
            'id'=>'required|integer',
            'rate'=>'required|numeric'
        ];
        $this->verify($request->all(),$rules);
        try{
            $logic = new CurrencyLogic();
            $result = $logic->editRate($request->all());
            ret_json('ok','修改成功');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //增加汇率
    public function addRate(Request $request)
    {
      $request->validate(
          [
              'currency'=>'required|string',
              'rate'=>'required|numeric',
          ],
          [
              'currency.required'=>'币种不能为空',
              'rate.required'=>'汇率不能为空'
          ]
      );
        try{
            $logic = new CurrencyLogic();
            $result = $logic->addRate($request);
            ret_json('ok','添加成功');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
}
